package com.tbycoder.tby.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbycoder.tby.config.AccessToken;
import com.tbycoder.tby.entity.Request.CommonIdRequest;
import com.tbycoder.tby.entity.Request.GetContentRequest;
import com.tbycoder.tby.entity.Request.PageRequest;
import com.tbycoder.tby.entity.Request.PublishRequest;
import com.tbycoder.tby.entity.Vo.GetTopicContextVo;
import com.tbycoder.tby.entity.Vo.LoginVo;
import com.tbycoder.tby.entity.Vo.ResourceVo;
import com.tbycoder.tby.excption.BizException;
import com.tbycoder.tby.service.IIposDiscussionPublisherService;
import com.tbycoder.tby.utils.ApiResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tby
 * @since 2021-05-13
 */
@RestController
@RequestMapping("/tby/ipos-discussion-publisher")
@CrossOrigin
public class IposDiscussionPublisherController {
    @Autowired
    IIposDiscussionPublisherService iIposDiscussionPublisherService;

    @AccessToken
    @PostMapping("/publish")
    @ApiOperation("发表主题和内容")
    public ApiResponse publish(
            @RequestBody PublishRequest publishRequest
    ){
//        LoginVo loginVo = null;
//        try {
//            loginVo = iIposLoginService.login(loginRequest);
//        }catch (BizException e){
//            return ApiResponse.error(e.getData());
//        }
//        return ApiResponse.success(loginVo);
        try {
            iIposDiscussionPublisherService.publish(publishRequest);
        }catch(BizException e){
            return ApiResponse.error(e.getData());
        }
        return ApiResponse.success();
    }
    
    @PostMapping("/getTopic")
    @ApiOperation("获取主题")
    public ApiResponse<Page<GetTopicContextVo>> getTopic(
            @RequestBody PageRequest pageRequest
            ){
        Page<GetTopicContextVo> getTopicContextVoPage = new Page<>();
        try{
            getTopicContextVoPage = iIposDiscussionPublisherService.getTopic(pageRequest);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.error(e.getMessage());
        }
        return ApiResponse.success(getTopicContextVoPage);
    }
    @PostMapping("/getContext")
    @ApiOperation("获取主题对应的内容")
    public ApiResponse<GetTopicContextVo> getTopic(
            @RequestBody GetContentRequest getContentRequest
            ){
        GetTopicContextVo getTopicContextVo= iIposDiscussionPublisherService.getContext(getContentRequest);
        
        return ApiResponse.success(getTopicContextVo);
    }
    @AccessToken
    @PostMapping("/returnContent")
    @ApiOperation("回复人回复")
    public ApiResponse returnContent(
            @RequestBody GetContentRequest getContentRequest
    ){
        try {
            iIposDiscussionPublisherService.returnContent(getContentRequest);
        }catch(BizException e){
            return ApiResponse.error(e.getData());
        }
        return ApiResponse.success();
    }

    @PostMapping("/deleteTopic")
    @ApiOperation("删除主题")
    public ApiResponse deleteTopic(
            @RequestBody GetContentRequest getContentRequest
    ){
        iIposDiscussionPublisherService.deleteTopic(getContentRequest);
        return ApiResponse.success();
    }

    
}
